﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text.RegularExpressions;
using System.Threading;
using Newtonsoft.Json;
using ReLogic.Graphics;
using Terraria.ModLoader;
using Terraria.Utilities;

namespace Terraria.Localization
{
	// Token: 0x02000321 RID: 801
	public class LanguageManager
	{
		// Token: 0x14000034 RID: 52
		// (add) Token: 0x06001E09 RID: 7689 RVA: 0x00440D88 File Offset: 0x0043EF88
		// (remove) Token: 0x06001E0A RID: 7690 RVA: 0x00440DC0 File Offset: 0x0043EFC0
		public event LanguageChangeCallback OnLanguageChanging;

		// Token: 0x14000035 RID: 53
		// (add) Token: 0x06001E0B RID: 7691 RVA: 0x00440DF8 File Offset: 0x0043EFF8
		// (remove) Token: 0x06001E0C RID: 7692 RVA: 0x00440E30 File Offset: 0x0043F030
		public event LanguageChangeCallback OnLanguageChanged;

		// Token: 0x17000215 RID: 533
		// (get) Token: 0x06001E0D RID: 7693 RVA: 0x000154D4 File Offset: 0x000136D4
		// (set) Token: 0x06001E0E RID: 7694 RVA: 0x000154DC File Offset: 0x000136DC
		public GameCulture ActiveCulture { get; private set; }

		// Token: 0x06001E0F RID: 7695 RVA: 0x000154E5 File Offset: 0x000136E5
		private LanguageManager()
		{
			this._localizedTexts[""] = LocalizedText.Empty;
		}

		// Token: 0x06001E10 RID: 7696 RVA: 0x00015523 File Offset: 0x00013723
		public int GetCategorySize(string name)
		{
			if (this._categoryGroupedKeys.ContainsKey(name))
			{
				return this._categoryGroupedKeys[name].Count;
			}
			return 0;
		}

		// Token: 0x06001E11 RID: 7697 RVA: 0x00440E68 File Offset: 0x0043F068
		public void SetLanguage(int legacyId)
		{
			GameCulture language = GameCulture.FromLegacyId(legacyId);
			this.SetLanguage(language);
		}

		// Token: 0x06001E12 RID: 7698 RVA: 0x00440E84 File Offset: 0x0043F084
		public void SetLanguage(string cultureName)
		{
			GameCulture language = GameCulture.FromName(cultureName);
			this.SetLanguage(language);
		}

		// Token: 0x06001E13 RID: 7699 RVA: 0x00440EA0 File Offset: 0x0043F0A0
		private void SetAllTextValuesToKeys()
		{
			foreach (KeyValuePair<string, LocalizedText> keyValuePair in this._localizedTexts)
			{
				keyValuePair.Value.SetValue(keyValuePair.Key);
			}
		}

		// Token: 0x06001E14 RID: 7700 RVA: 0x00440F00 File Offset: 0x0043F100
		private string[] GetLanguageFilesForCulture(GameCulture culture)
		{
			Assembly.GetExecutingAssembly();
			return Array.FindAll<string>(typeof(Program).Assembly.GetManifestResourceNames(), (string element) => element.StartsWith("Terraria.Localization.Content." + culture.CultureInfo.Name) && element.EndsWith(".json"));
		}

		// Token: 0x06001E15 RID: 7701 RVA: 0x00440F48 File Offset: 0x0043F148
		public void SetLanguage(GameCulture culture)
		{
			if (this.ActiveCulture == culture)
			{
				return;
			}
			if (culture != this._fallbackCulture && this.ActiveCulture != this._fallbackCulture)
			{
				this.SetAllTextValuesToKeys();
				this.LoadLanguage(this._fallbackCulture);
			}
			this.LoadLanguage(culture);
			if (this.ActiveCulture != null)
			{
				ModLoader.RefreshModLanguage(culture);
			}
			this.ActiveCulture = culture;
			Thread.CurrentThread.CurrentCulture = culture.CultureInfo;
			Thread.CurrentThread.CurrentUICulture = culture.CultureInfo;
			if (this.OnLanguageChanged != null)
			{
				this.OnLanguageChanged(this);
			}
		}

		// Token: 0x06001E16 RID: 7702 RVA: 0x00015546 File Offset: 0x00013746
		private void LoadLanguage(GameCulture culture)
		{
			this.LoadFilesForCulture(culture);
			if (this.OnLanguageChanging != null)
			{
				this.OnLanguageChanging(this);
			}
			this.ProcessCopyCommandsInTexts();
		}

		// Token: 0x06001E17 RID: 7703 RVA: 0x00440FD8 File Offset: 0x0043F1D8
		private void LoadFilesForCulture(GameCulture culture)
		{
			string[] languageFilesForCulture = this.GetLanguageFilesForCulture(culture);
			foreach (string text in languageFilesForCulture)
			{
				try
				{
					string text2 = LanguageManager.ReadEmbeddedResource(text);
					if (text2 == null || text2.Length < 2)
					{
						throw new FileFormatException();
					}
					this.LoadLanguageFromFileText(text2);
				}
				catch (Exception)
				{
					if (Debugger.IsAttached)
					{
						Debugger.Break();
					}
					Console.WriteLine("Failed to load language file: " + text);
					break;
				}
			}
		}

		// Token: 0x06001E18 RID: 7704 RVA: 0x00441058 File Offset: 0x0043F258
		private static string ReadEmbeddedResource(string path)
		{
			string result;
			using (Stream manifestResourceStream = Assembly.GetExecutingAssembly().GetManifestResourceStream(path))
			{
				using (StreamReader streamReader = new StreamReader(manifestResourceStream))
				{
					result = streamReader.ReadToEnd();
				}
			}
			return result;
		}

		// Token: 0x06001E19 RID: 7705 RVA: 0x004410B4 File Offset: 0x0043F2B4
		internal void ProcessCopyCommandsInTexts()
		{
			Regex regex = new Regex("{\\$([\\w\\.]+)}", RegexOptions.Compiled);
			foreach (KeyValuePair<string, LocalizedText> keyValuePair in this._localizedTexts)
			{
				LocalizedText value = keyValuePair.Value;
				if (value.Value != null)
				{
					for (int i = 0; i < 100; i++)
					{
						string text = regex.Replace(value.Value, (Match match) => this.GetTextValue(match.Groups[1].ToString()));
						if (text == value.Value)
						{
							break;
						}
						value.SetValue(text);
					}
				}
			}
		}

		// Token: 0x06001E1A RID: 7706 RVA: 0x00441160 File Offset: 0x0043F360
		public void LoadLanguageFromFileText(string fileText)
		{
			Dictionary<string, Dictionary<string, string>> dictionary = JsonConvert.DeserializeObject<Dictionary<string, Dictionary<string, string>>>(fileText);
			foreach (KeyValuePair<string, Dictionary<string, string>> keyValuePair in dictionary)
			{
				string key = keyValuePair.Key;
				foreach (KeyValuePair<string, string> keyValuePair2 in keyValuePair.Value)
				{
					string key2 = keyValuePair.Key + "." + keyValuePair2.Key;
					if (this._localizedTexts.ContainsKey(key2))
					{
						this._localizedTexts[key2].SetValue(keyValuePair2.Value);
					}
					else
					{
						this._localizedTexts.Add(key2, new LocalizedText(key2, keyValuePair2.Value));
						if (!this._categoryGroupedKeys.ContainsKey(keyValuePair.Key))
						{
							this._categoryGroupedKeys.Add(keyValuePair.Key, new List<string>());
						}
						this._categoryGroupedKeys[keyValuePair.Key].Add(keyValuePair2.Key);
					}
				}
			}
		}

		// Token: 0x06001E1B RID: 7707 RVA: 0x004412C0 File Offset: 0x0043F4C0
		[Conditional("DEBUG")]
		private void ValidateAllCharactersContainedInFont(DynamicSpriteFont font)
		{
			if (font == null)
			{
				return;
			}
			foreach (LocalizedText localizedText in this._localizedTexts.Values)
			{
				foreach (char c in localizedText.Value)
				{
				}
			}
		}

		// Token: 0x06001E1C RID: 7708 RVA: 0x00441338 File Offset: 0x0043F538
		public LocalizedText[] FindAll(Regex regex)
		{
			int num = 0;
			foreach (KeyValuePair<string, LocalizedText> keyValuePair in this._localizedTexts)
			{
				if (regex.IsMatch(keyValuePair.Key))
				{
					num++;
				}
			}
			LocalizedText[] array = new LocalizedText[num];
			int num2 = 0;
			foreach (KeyValuePair<string, LocalizedText> keyValuePair2 in this._localizedTexts)
			{
				if (regex.IsMatch(keyValuePair2.Key))
				{
					array[num2] = keyValuePair2.Value;
					num2++;
				}
			}
			return array;
		}

		// Token: 0x06001E1D RID: 7709 RVA: 0x00441400 File Offset: 0x0043F600
		public LocalizedText[] FindAll(LanguageSearchFilter filter)
		{
			LinkedList<LocalizedText> linkedList = new LinkedList<LocalizedText>();
			foreach (KeyValuePair<string, LocalizedText> keyValuePair in this._localizedTexts)
			{
				if (filter(keyValuePair.Key, keyValuePair.Value))
				{
					linkedList.AddLast(keyValuePair.Value);
				}
			}
			return linkedList.ToArray<LocalizedText>();
		}

		// Token: 0x06001E1E RID: 7710 RVA: 0x0044147C File Offset: 0x0043F67C
		public LocalizedText SelectRandom(LanguageSearchFilter filter, UnifiedRandom random = null)
		{
			int num = 0;
			foreach (KeyValuePair<string, LocalizedText> keyValuePair in this._localizedTexts)
			{
				if (filter(keyValuePair.Key, keyValuePair.Value))
				{
					num++;
				}
			}
			int num2 = (random ?? Main.rand).Next(num);
			foreach (KeyValuePair<string, LocalizedText> keyValuePair2 in this._localizedTexts)
			{
				if (filter(keyValuePair2.Key, keyValuePair2.Value) && --num == num2)
				{
					return keyValuePair2.Value;
				}
			}
			return LocalizedText.Empty;
		}

		// Token: 0x06001E1F RID: 7711 RVA: 0x00441564 File Offset: 0x0043F764
		public LocalizedText RandomFromCategory(string categoryName, UnifiedRandom random = null)
		{
			if (!this._categoryGroupedKeys.ContainsKey(categoryName))
			{
				return new LocalizedText(categoryName + ".RANDOM", categoryName + ".RANDOM");
			}
			List<string> list = this._categoryGroupedKeys[categoryName];
			return this.GetText(categoryName + "." + list[(random ?? Main.rand).Next(list.Count)]);
		}

		// Token: 0x06001E20 RID: 7712 RVA: 0x00015569 File Offset: 0x00013769
		public bool Exists(string key)
		{
			return this._localizedTexts.ContainsKey(key);
		}

		// Token: 0x06001E21 RID: 7713 RVA: 0x00015577 File Offset: 0x00013777
		public LocalizedText GetText(string key)
		{
			if (!this._localizedTexts.ContainsKey(key))
			{
				return new LocalizedText(key, key);
			}
			return this._localizedTexts[key];
		}

		// Token: 0x06001E22 RID: 7714 RVA: 0x0001559B File Offset: 0x0001379B
		public string GetTextValue(string key)
		{
			if (this._localizedTexts.ContainsKey(key))
			{
				return this._localizedTexts[key].Value;
			}
			return key;
		}

		// Token: 0x06001E23 RID: 7715 RVA: 0x000155BE File Offset: 0x000137BE
		public string GetTextValue(string key, object arg0)
		{
			if (this._localizedTexts.ContainsKey(key))
			{
				return this._localizedTexts[key].Format(arg0);
			}
			return key;
		}

		// Token: 0x06001E24 RID: 7716 RVA: 0x000155E2 File Offset: 0x000137E2
		public string GetTextValue(string key, object arg0, object arg1)
		{
			if (this._localizedTexts.ContainsKey(key))
			{
				return this._localizedTexts[key].Format(arg0, arg1);
			}
			return key;
		}

		// Token: 0x06001E25 RID: 7717 RVA: 0x00015607 File Offset: 0x00013807
		public string GetTextValue(string key, object arg0, object arg1, object arg2)
		{
			if (this._localizedTexts.ContainsKey(key))
			{
				return this._localizedTexts[key].Format(arg0, arg1, arg2);
			}
			return key;
		}

		// Token: 0x06001E26 RID: 7718 RVA: 0x0001562E File Offset: 0x0001382E
		public string GetTextValue(string key, params object[] args)
		{
			if (this._localizedTexts.ContainsKey(key))
			{
				return this._localizedTexts[key].Format(args);
			}
			return key;
		}

		// Token: 0x06001E27 RID: 7719 RVA: 0x00015652 File Offset: 0x00013852
		public void SetFallbackCulture(GameCulture culture)
		{
			this._fallbackCulture = culture;
		}

		// Token: 0x04001942 RID: 6466
		public static LanguageManager Instance = new LanguageManager();

		// Token: 0x04001943 RID: 6467
		internal Dictionary<string, LocalizedText> _localizedTexts = new Dictionary<string, LocalizedText>();

		// Token: 0x04001944 RID: 6468
		private Dictionary<string, List<string>> _categoryGroupedKeys = new Dictionary<string, List<string>>();

		// Token: 0x04001945 RID: 6469
		private GameCulture _fallbackCulture = GameCulture.English;
	}
}
